/*
 * @Author: your name
 * @Date: 2021-02-16 17:28:41
 * @LastEditTime: 2021-03-02 15:54:30
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \jianfa-api\service\staCallServce.js
 */
const { callDao, getTotalDao } = require('../dao/staCallDao');
const moment = require('moment')
moment.locale("zh-cn");
module.exports = {
 //发起一个统计
 async callService(ip) {
  const obj = {
   ip
  }
  const create_time = moment().format('YYYY年MM月D日 h:mm:ss');
  obj.create_time = create_time
  return await callDao(obj)
 },
 //获取统计信息
 async getTotalService() {
  const { status, results } = await getTotalDao()
  const totalObj = {
   total: results.length,
   today_total: '',
   year_months: {
    current_year: '',
    months: {}
   },//一年每个月的访问量
   month_days: {
    current_month: '',
    days: {}
   },//当前月每天的访问量
  }
  // 今日访问量
  moment().format('MM月D日')
  const todayDate = results.filter(({ create_time }) => {
   return create_time.match(new RegExp(moment().format('MM月D日')))
  })
  totalObj['today_total'] = todayDate.length
  //一年每个月的访问量
  // create_time.match(new RegExp(moment().format('DD日')))
  for (let i = 1; i <= 12; i++) {
   const dateStr = i.toString().length < 2 ? ('0' + i + '月') : (i + '月')
   const year = moment().format('YYYY年')//YYYY年
   let month_total = 0
   results.forEach(({ create_time }) => {
    if (create_time.match(new RegExp('^' + year + dateStr))) {
     month_total++
    }
   })
   totalObj['year_months']['current_year'] = moment().format('YYYY年')
   totalObj['year_months']['months'][i] = month_total
  }
  //当前月每天的访问量
  for (let i = 1; i <= 31; i++) {
   // const dateStr = i.toString().length < 2 ? ('0' + i + '日') : (i + '日')
   const dateStr = (i + '日')
   const year_month = moment().format('YYYY年MM月')//YYYY年
   let day_total = 0
   results.forEach(({ create_time }) => {
    if (create_time.match(new RegExp('^' + year_month + dateStr))) {
     day_total++
    }
   })
   totalObj['month_days']['current_month'] = moment().format('MM月')
   totalObj['month_days']['days'][i] = day_total
  }
  return { status, results: totalObj }

 }
}